package mall.service;

import mall.dao.MallOrderDao;
import mall.dao.MallOrderDaoImpl;
import mall.model.bo.MallAddOrderBO;
import mall.model.bo.MallSendContentBO;
import mall.model.bo.MallSettleAccountsBO;
import mall.model.tempob.CommentInfoFromOrder;
import mall.model.tempob.OrderInfoFromSpec;
import mall.model.tempob.OrderInfoFromUser;
import mall.model.vo.MallGetOrderByStateVO;
import mall.model.vo.MallOrderGoodsInfoVO;
import mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 21:07
 *
 * */
public class MallOrderServiceImpl implements MallOrderService {

    private MallOrderDao mallOrderDao = new MallOrderDaoImpl();

    @Override
    public void addOrder(MallAddOrderBO addOrderBO) {
        OrderInfoFromUser user = mallOrderDao.getOrderInfoFromUser(addOrderBO.getToken());
        OrderInfoFromSpec spec = mallOrderDao.getOrderInfoFromSpec(addOrderBO.getGoodsDetailId());
        String goods = mallOrderDao.getOrderInfoFromGoods(spec.getGoodsId());

        //从生成订单到库存的减少的操作，必须具有原子性，因此需要在此处添加事物
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(true);
            connection.setAutoCommit(false);
            //生成订单数据
            mallOrderDao.addOrder(addOrderBO, user, spec, goods);

            //测试事物是否正常运行
             // double test = 1 / 0;
            //获取当前商品的库存
            String stockNum = mallOrderDao.getStockNum(addOrderBO.getGoodsDetailId());
            //对应spec表中的商品减少
            mallOrderDao.reduceSpec(addOrderBO.getGoodsDetailId(), Integer.parseInt(stockNum) - addOrderBO.getNum());
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                //将threadlocal中的connection清空
                DruidUtils.releaseConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public List<MallGetOrderByStateVO> getOrderByState(String state, String token) {
        //从订单表获取部分订单数据信息
        List<MallGetOrderByStateVO> orders = mallOrderDao.getOrderByStateFromOrders(state, token);
        //从goods表获取部分订单数据信息，并封装到orders变量中
        for (MallGetOrderByStateVO order : orders) {
            //从spec表获取获取goodsId, spec
            OrderInfoFromSpec orderInfoFromSpec = mallOrderDao.getSpecInfo(order.getGoodsDetailId());
            //从goods表获取其他商品信息
            MallOrderGoodsInfoVO goodsInfoVO = mallOrderDao.getOderByStateFromGoods(orderInfoFromSpec.getGoodsId());
            //封装goodsInfoVO的其他属性
            goodsInfoVO.setGoodsDetailId(order.getGoodsDetailId());
            goodsInfoVO.setSpec(orderInfoFromSpec.getSpec());

            //封装到order中
            order.setGoods(goodsInfoVO);
        }

        return orders;
    }

    @Override
    public void deleteOrder(String id) {
        mallOrderDao.deleteOrder(id);
    }

    @Override
    public void settleAccounts(List<MallSettleAccountsBO> settleAccountsBOS) {
        mallOrderDao.settleAccounts(settleAccountsBOS);
    }

    @Override
    public void pay(String id) {
        mallOrderDao.pay(id);
    }

    @Override
    public void confirmReceive(String id) {
        mallOrderDao.confirmReceive(id);
    }

    @Override
    public void sendComment(MallSendContentBO sendContentBO) {
        //根据orderId跟新order表中的hasComment，updatetime等字段信息，同时获取userId，specName字段数据
        CommentInfoFromOrder commentInfo = mallOrderDao.updateOrderOfHasComment(sendContentBO.getOrderId());
        //更新comments表信息，即新建comment内容
        mallOrderDao.sendComment(sendContentBO, commentInfo);
    }
}
